package com.zj.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zj.model.Orders;
import com.zj.web.bean.OrdersDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {
    //使用动态注解方式,userId为null时,查询所有订单
    @Select("<script>" +
            "SELECT ods.*, a.recipientName, a.phone, a.detailedAddress, " +
            "p.productName, p.productImg, p.description, p.price, od.count " +
            "FROM orders ods " +
            "LEFT JOIN orderdetail od ON ods.orderId = od.orderId " +
            "LEFT JOIN product p ON od.productId = p.productId " +
            "LEFT JOIN address a ON ods.addressId = a.addressId " +
            "WHERE ods.orderStatus = #{orderStatus} " +
            "<if test='userId != null'>" +
            "AND ods.userId = #{userId} " +
            "</if>" +
            "ORDER BY ods.orderId DESC" +
            "</script>")
    @Results({
            @Result(column = "detailId", property = "orderdetail.detailId"),
            @Result(column = "orderId", property = "orderId"),
            @Result(column = "userId", property = "userId"),
            @Result(column = "addressId", property = "addressId"),
            @Result(column = "orderTime", property = "orderTime"),
            @Result(column = "orderStatus", property = "orderStatus"),
            @Result(column = "recipientName", property = "address.recipientName"),
            @Result(column = "phone", property = "address.phone"),
            @Result(column = "detailedAddress", property = "address.detailedAddress"),
            @Result(column = "productName", property = "product.productName"),
            @Result(column = "productImg", property = "product.productImg"),
            @Result(column = "description", property = "product.description"),
            @Result(column = "price", property = "orderdetail.currentPrice"),
            @Result(column = "count", property = "orderdetail.count")
    })
    List<OrdersDTO> selectByOrderStatusWithProduct(@Param("orderStatus") Integer orderStatus,
                                                   @Param("userId") Long userId);

    @Select("select ods.*,a.recipientName,a.phone,a.detailedAddress,\n" +
            "        p.productName,p.productImg,p.description, p.price, od.count\n" +
            "from orders ods\n" +
            "left join orderdetail od on ods.orderId = od.orderId\n" +
            "left join product p on od.productId = p.productId\n" +
            "left join address a on ods.addressId=a.addressId\n" +
            "where ods.orderId=#{orderId}\n" +
            "order by ods.orderId desc;")
    @Results({
            //property需要严格对照bean类的信息
            @Result(column = "detailId", property = "orderdetail.detailId"),
            @Result(column = "orderId", property = "orderId"),
            @Result(column = "userId", property = "userId"),
            @Result(column = "addressId", property = "addressId"),
            @Result(column = "orderTime", property = "orderTime"),
            @Result(column = "categoryStatus", property = "categoryStatus"),
            @Result(column = "productName", property = "product.productName"),
            @Result(column = "productImg", property = "product.productImg"),
            @Result(column = "description", property = "product.description"),
            @Result(column = "price", property = "orderdetail.currentPrice"),
            @Result(column = "count", property = "orderdetail.count")
    })
    List<OrdersDTO> selectByOrderIdWithProduct(@Param("orderId") Long orderId);


}
